<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace backend\controllers;


use bc\controller\BackendController;
use bc\model\OrderModel;
use bc\util\Util;
use yii;
use bc\util\backend\Page;

class VShopOrderController extends BackendController
{
    private static $product_id = 3;
    protected $arrStatus = [1 => '未支付', 2 => '已支付', 3 => '已发货'];
    
    public function actionIndex(){
        $model = new OrderModel();
        $sort_pay_time = $this->get('sort_pay_time', '');
        $sort_time = $this->get('sort_time', '');

        if(($sort_pay_time == '') && ($sort_time == '')){
            $sort = ' bc_order.create_time desc';
        }elseif($sort_pay_time == ''){
            if($sort_time == 'desc'){
                $sort = ' bc_order.create_time desc';
            }elseif($sort_time == 'asc'){
                $sort = 'bc_order.create_time asc';
            }else{
                $sort = 'bc_order.create_time desc';
            }
        }elseif($sort_time == ''){
            if($sort_pay_time == 'desc'){
                $sort = ' bc_order.pay_time desc';
            }elseif($sort_pay_time == 'asc'){
                $sort = 'bc_order.pay_time asc';
            }else{
                $sort = 'bc_order.pay_time desc';
            }
        }else{
            $sort = ' bc_order.create_time,bc_order.pay_time desc';
        }

        $start_time_str = $this->get('start_time', '');
        $end_time_str = $this->get('end_time', '');
        $start_time = $start_time_str ? strtotime($start_time_str) : '';
        $end_time = $end_time_str ? strtotime($end_time_str) : '';
        $params = ' bc_order_detail.product_id = '. self::$product_id .' ';
        if($start_time != '')
        {
            $params = $params." and bc_order.create_time >= {$start_time}";
        }

        if($end_time != '')
        {
            $end_time+=60*60*24;
            $params = $params." and bc_order.create_time <= {$end_time}";
        }

        //支付时间
        $pay_start_time_str = $this->get('pay_start_time', '');
        $pay_end_time_str = $this->get('pay_end_time', '');
        $pay_start_time = $pay_start_time_str ? strtotime($pay_start_time_str) : '';
        $pay_end_time = $pay_end_time_str ? strtotime($pay_end_time_str) : '';
        if($pay_start_time != '')
        {
            $params = $params." and bc_order.pay_time >= {$pay_start_time}";
        }

        if($pay_end_time != '')
        {
            $pay_end_time+=60*60*24;
            $params = $params." and bc_order.pay_time <= {$pay_end_time}";
        }


        //状态
        $order_status = $this->get('order_status', '');
        if($order_status != 0)
        {
            $params = $params." and bc_order.order_status = {$order_status}";
        }

        //状态
        $mobile = $this->get('mobile', '');
        if($mobile != 0)
        {
            $params = $params." and ecs_users.mobile = '{$mobile}'";
        }

        $page = $this->get('page', 1);
        $model->sort=$sort;
        $model->columns='bc_order.order_id as oid,bc_order.create_time as ocreate_time,bc_order.pay_time as opay_time,bc_product.product_id as pid,bc_order_detail.order_detail_id as did,bc_address.id as aid,bc_wx_user.id as wid,ecs_users.user_id as uid,bc_order_status.order_status_id as sid,bc_address.city as city_name,bc_address.province as province_name,bc_address.district as district_name,bc_address.address as address_name,bc_order.*,bc_product.*,bc_order_detail.*,bc_address.*,bc_wx_user.*,ecs_users.*,bc_order_status.*';
        $list = $model->getList($params, $page, self::PAGE_SIZE);
        $total = $model->getTotal($params);
        $pagination = new Page($total, self::PAGE_SIZE);

        $view['list'] = $list;
        $view['pagination'] = $pagination;
        $view['start_time'] = $start_time_str;
        $view['end_time'] = $end_time_str;
        $view['pay_start_time'] = $pay_start_time_str;
        $view['pay_end_time'] = $pay_end_time_str;
        $view['order_status'] = $order_status;
        $view['mobile'] = $mobile;
        $view['arrStatus'] = $this->arrStatus;
        return $this->render('index',$view);
    }
    
    
    
    
    
    public function actionOrderinfo(){
        $order_id = $this->get('id', '');
        if(empty($order_id)){
            $params['msg'] = '无order_id!';
            $params['url'] = '/shop-order/index';
            $this->forward($params);
        }
        $model = new OrderModel();
        $orderinfo = $model->getRow(['bc_order.order_id'=>$order_id]);
        $view['orderinfo']=$orderinfo[0];
        
        return $this->render('orderinfo',$view);
    }
    
    
}